<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}"  media="all">
    <script th:src="@{/jquery/jquery-3.3.1.js}" type="text/javascript"></script>
    <script th:src="@{/layui/layui.js}" type="text/javascript"></script>
    <script th:src="@{/common/layuiload.js}" type="text/javascript"></script>
    <title>分配</title>
    <style>
    /*css*/
    </style>

</head>
<body style="margin-left:20px;">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>分配</legend>
</fieldset>
<form id="comitform" class="layui-form" method="post">
    <input type="hidden" th:value="${roleid}" name="roleid" id="roleid">
    <div class="layui-form-item" pane="">
        <label class="layui-form-label">资源</label>
        <input type="checkbox" lay-skin="primary" lay-filter="allChoose" title="全选">
        <div class="layui-input-block" th:each="r:${perms}">
             <input type="checkbox" name="permids" th:value="${r.id}" lay-skin="primary" th:title="${r.name}+':'+${r.url}+':'+${r.desc}" >
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="demo1" type="button" onclick="formsubmit()">立即提交</button>
        </div>
    </div>
</form>
<!-- 获取basepath url ajax使用 -->
<span th:include="fragments/basepath::html"></span>
<!-- 获取basepath url ajax使用 -->
</body>
<script th:inline="javascript">
    var baseUrl = getBaseUrl();
    $(function () {
        checkboxshow();
    });
    function checkboxshow(){
        var hasperms =[[${hasperms}]];
        $("input:checkbox[name='permids']").each(function () {
            var rid =$(this).val();
            for(var i=0;i<hasperms.length;i++){
                if(rid==hasperms[i].id){//值相同
                    $(this).attr("checked","true");
                }
            }
        });
    }
    //js
    function formsubmit(){
        var roleid=$("#roleid").val();
        var perms="";
       $("input:checkbox[name='permids']:checked").each(function () {
           perms+=$(this).val()+",";
       });
       if(perms!="" && roleid!=""){
           $.ajax({
                url:baseUrl+"/perm/rolesconfigDo",
                data:{"perms":perms,"roleid":roleid},
                type:"post",
                dataType:"json",
                success:function(data){
                    if(data==0){
                        alert("操作失败!");
                    }
                    if(data==1){
                        alert("操作成功");
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }
                }
            });
       }
    }
    /**
     * 动态赋值 否则无法使用功能
     */
    layui.use('form', function() {
        var form = layui.form;
        form.on('checkbox(allChoose)', function (data) {
            $("input[name='permids']").each(function () {
                this.checked = data.elem.checked;
            });
            form.render('checkbox');
        });
    });

</script>
</html>